package com.fenghuajueli.libbasecoreui.upload.honor

import android.app.Application
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.lifecycleScope
import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.LogUtils
import com.fenghuajueli.libbasecoreui.upload.BaseUpload
import com.fenghuajueli.libbasecoreui.upload.BrandUtil
import com.fenghuajueli.libbasecoreui.upload.Channel
import com.fenghuajueli.libbasecoreui.utils.MmkvUtils
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.util.*


class HonorUploadUtils : BaseUpload() {

    companion object {
        @JvmStatic
        val instance: HonorUploadUtils by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
            HonorUploadUtils()
        }
    }

    /**
     * 打开应用时检查
     * 首次安装
     * 次日留存
     * @param lifecycleOwner
     * @param application
     */
    fun LifecycleOwner.openAction(application: Application) {
        if (BrandUtil.isHonor()) {
            action(channel = Channel.HONOR) {
                if (checkIsFirstInstall(application)) {
                    MmkvUtils.save(FIRST_LAUNCH, false)
                    LogUtils.d("firstInstallAction: 首次安装启动")
                    uploadActiveData(application, HonorDataType.ACTIVATE_TYPE, null)
                } else if (checkSeconddayRetention()) {
                    LogUtils.d("firstInstallAction: 次留")
                    uploadActiveData(application, HonorDataType.TOW_DAY, null)
                }
            }
        }

    }

    fun openAction2(owner: LifecycleOwner, application: Application) {
        if (BrandUtil.isHonor()) {
            owner.lifecycleScope.launch(Dispatchers.IO) {
                if (checkIsFirstInstall(application)) {
                    MmkvUtils.save(FIRST_LAUNCH, false)
                    LogUtils.d("firstInstallAction: 首次安装启动")
                    uploadActiveData(application, HonorDataType.ACTIVATE_TYPE, null)
                } else if (checkSeconddayRetention()) {
                    LogUtils.d("firstInstallAction: 次留")
                    uploadActiveData(application, HonorDataType.TOW_DAY, null)
                }
            }
        }

    }

    fun openAction(application: Application) {
        appScope.launch {
            action(Channel.HONOR) {
                if (checkIsFirstInstall(application)) {
                    MmkvUtils.save(FIRST_LAUNCH, false)
                    LogUtils.d("firstInstallAction: 首次安装启动")
                    uploadActiveData(application, HonorDataType.ACTIVATE_TYPE, null)
                } else if (checkSeconddayRetention()) {
                    LogUtils.d("firstInstallAction: 次留")
                    uploadActiveData(application, HonorDataType.TOW_DAY, null)
                }
            }
        }
    }


    /**
     * 注册
     * 注册完3分钟内算新注册用户
     * @param needCheck 一键登录、微信登录需要传true
     * @param application
     */
    fun LifecycleOwner.registerAction(application: Application, needCheck: Boolean = false) {
        if (BrandUtil.isHonor()) {
            action(channel = Channel.HONOR) {
                if (needCheck) {
                    if (checkIsRegister()) {
                        uploadActiveData(application, HonorDataType.REGISTERED, null)
                    }
                } else {
                    uploadActiveData(application, HonorDataType.REGISTERED, null)
                }
            }
        }

    }

    fun registerAction(application: Application) {
        appScope.launch {
            action(Channel.HONOR) {
                uploadActiveData(application, HonorDataType.REGISTERED, null)
            }
        }
    }

    fun registerAction2(
        owner: LifecycleOwner,
        application: Application,
        needCheck: Boolean = false
    ) {
        if (BrandUtil.isHonor()) {
            owner.lifecycleScope.launch(Dispatchers.IO) {
                if (needCheck) {
                    if (checkIsRegister()) {
                        uploadActiveData(application, HonorDataType.REGISTERED, null)
                    }
                } else {
                    uploadActiveData(application, HonorDataType.REGISTERED, null)
                }
            }
        }

    }


    /**
     * 付费
     * 7天内付费才上传
     * @param lifecycleOwner
     * @param application
     * @param payAmount
     */
    fun LifecycleOwner.payAction(application: Application, payAmount: Double) {
        if (BrandUtil.isHonor()) {
            action(channel = Channel.HONOR) {
                if (checkPayWithinSevendays()) {
                    uploadActiveData(application, HonorDataType.APP_PAY, (payAmount * 100).toLong())
                }
            }
        }

    }

    fun payAction(application: Application, payAmount: Double) {
        appScope.launch {
            action(Channel.HONOR) {
                if (checkPayWithinSevendays()) {
                    uploadActiveData(application, HonorDataType.APP_PAY, (payAmount * 100).toLong())
                }
            }
        }
    }

    fun payAction2(owner: LifecycleOwner, application: Application, payAmount: Double) {
        if (BrandUtil.isHonor()) {
            owner.lifecycleScope.launch(Dispatchers.IO) {
                if (checkPayWithinSevendays()) {
                    uploadActiveData(
                        application,
                        HonorDataType.APP_PAY,
                        (payAmount * 100).toLong()
                    )
                }
            }
        }

    }


    /**
     * oppo转化数据同步接口
     *
     * @param dataType
     * @param payAmount
     */
    private suspend fun uploadActiveData(
        application: Application,
        @HonorDataType dataType: Int,
        payAmount: Long?
    ) {

        application.uploadHonorMonitorData(dataType) {
            val contentsMap = hashMapOf<String, Any>()
            contentsMap.apply {
                contentsMap["app_pkg"] = application.packageName
                contentsMap["channel"] = 11
                contentsMap["data_type"] = dataType
                contentsMap["oaid"] = oaid!!
                contentsMap["timestamp"] = System.currentTimeMillis()
                payAmount?.also { contentsMap["price"] = it }
            }

            GsonUtils.toJson(contentsMap)
        }
    }

}